package com.colt.contentcenter.sentineldemo;

import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlCleaner;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.math.NumberUtils;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @program: content-center
 * @description:
 * @author: colt
 * @create: 2020-07-07 11:31
 **/
@Component
@Slf4j
public class MyUrlCleaner implements UrlCleaner {
    @Override
    public String clean(String originUrl) {
        //让shares/1与shares/2返回值相同
        //返回/shares/{number}
        String[] split = originUrl.split("/");
        return Arrays.stream(split)
              .map(s -> {
                  if (NumberUtils.isNumber(s)){
                        return "{number}";
                  }
                  return s;
                })
                .reduce((a,b)->a+"/"+b)
                .orElse("");
    }
}
